﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyVoteMVC.BackstageManagement.Views;
using MyVoteMVC.Service;
using MyVoteMVC.Common;
using System.IO;
using MyVoteMVC.Domain;
using System.Drawing;

namespace MyVoteMVC.BackstageManagement.Controllers
{
    public class ItemController:IItemController
    {
        IItemView view;
        IItemService itemService;
        IItemImageService itemImageService;
        IProjectService projectService;

        public ItemController(IItemView view)
        {
            this.view = view;
            projectService = Factory.GetInstance<IProjectService>();
            itemService = Factory.GetInstance<IItemService>();
            itemImageService = Factory.GetInstance<IItemImageService>();
        }

        public bool CreateItem(string questionID,
            string projectID,
            string itemTitle,
            string itemContent,
            string itemImage,
            int IfDefault)
        {
            //check is null
            if (string.IsNullOrWhiteSpace(itemTitle))
            {
                view.OnSubmitError("标题不为空");
                return false;
            }
            //if (string.IsNullOrWhiteSpace(itemContent))
            //{
            //    view.OnSubmitError("内容不为空");
            //    return false;
            //}

            if (Encoding.Default.GetByteCount(itemTitle) > 300)
            {
                view.OnSubmitError("标题过长");
                return false;
            }

            try
            {
                itemService.CreateItems(
                    questionID,
                    projectID,
                    itemTitle,
                    itemContent,
                    IfDefault,
                    "admin",
                 ReadFile(itemImage));
                view.CreateItemComplete("创建成功");
                return true;
            }
            catch (ArgumentException ex)
            {
                view.OnSubmitError(ex.Message);
                return false;
            }
            catch (Exception ex)
            {
                view.OnSubmitError(ex.Message);
                return false;
            }

        }

        public bool CreateItem(string questionID,
           string projectID,
           string itemTitle,
           string itemContent,
           byte[] itemImage,
           int IfDefault)
        {
            try
            {
                itemService.CreateItems(
                    questionID,
                    projectID,
                    itemTitle,
                    itemContent,
                    IfDefault,
                    "admin",
                 itemImage);
                view.CreateItemComplete("创建成功");
                return true;
            }
            catch (ArgumentException ex)
            {
                view.OnSubmitError(ex.Message);
                return false;
            }
            catch (Exception ex)
            {
                view.OnSubmitError(ex.Message);
                return false;
            }
        }

        public bool EditItem(string questionID,
            string projectID,
            string itemID,
            string itemTitle,
            string itemContent,
            string itemImage,
            int IfDefault)
        {
            if (string.IsNullOrWhiteSpace(itemTitle))
            {
                view.OnSubmitError("标题不为空");
                return false;
            }
            //if (string.IsNullOrWhiteSpace(itemContent))
            //{
            //    view.OnSubmitError("内容不为空");
            //    return false;
            //}

            if (Encoding.Default.GetByteCount(itemTitle) > 300)
            {
                view.OnSubmitError("标题过长");
                return false;
            }
            try
            {
                itemService.EditItems(
                    questionID,
                    projectID,
                    itemID,
                    itemTitle,
                    itemContent,
                    IfDefault,
                    "admin");

                byte[] img = ReadFile(itemImage);
                if (img != null)
                {
                    itemImageService.ModifyItemImage(itemID, img);
                }

                view.EditItemComplete("修改成功");
                return true;
            }
            catch (ArgumentException ex)
            {
                view.OnSubmitError(ex.Message);
                return false;
            }
            catch (Exception ex)
            {
                view.OnSubmitError(ex.Message);
                return false;
            }
        }

        public bool EditItem(string questionID,
           string projectID,
           string itemID,
           string itemTitle,
           string itemContent,
            byte[] itemImage,
           int IfDefault)
        {
            try
            {
                itemService.EditItems(
                    questionID,
                    projectID,
                    itemID,
                    itemTitle,
                    itemContent,
                    IfDefault,
                    "admin");

                byte[] img = itemImage;
                if (img != null)
                {
                    itemImageService.ModifyItemImage(itemID, img);
                }

                view.EditItemComplete("修改成功");
                return true;
            }
            catch (ArgumentException ex)
            {
                view.OnSubmitError(ex.Message);
                return false;
            }
            catch (Exception ex)
            {
                view.OnSubmitError(ex.Message);
                return false;
            }
        }

        public void GetItem(string questionID, string itemID)
        {
            view.GetItemComplete(itemService.GetItems(questionID, itemID));
        }

        private byte[] ReadFile(string fileName)
        {
            bool isBroken = false;
            try
            {
                Bitmap bmp = new Bitmap(fileName);
                if (bmp == null)
                {
                    //图片损坏
                    isBroken = true;
                }
            }
            catch //(Exception ex)
            {
                //图片损坏
                isBroken = true;
            }

            if (isBroken)
                return null;

            FileStream pFileStream = null;
            byte[] pReadByte = null;

            try
            {
                pFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                BinaryReader r = new BinaryReader(pFileStream);
                r.BaseStream.Seek(0, SeekOrigin.Begin);    //将文件指针设置到文件开
                pReadByte = r.ReadBytes((int)r.BaseStream.Length);
                return pReadByte;
            }
            catch
            {
                return pReadByte;
            }
            finally
            {
                if (pFileStream != null)
                    pFileStream.Close();
            }
        }


        public void GetProjects()
        {
            view.GetProjectsComplete(projectService.GetAllProjects());
        }

        public void CreateItems(List<VT_Items> items)
        {
            try
            {
                itemService.CreateItems(items, "Admin");
                view.CreateItemsComplete("创建成功");
            }
            catch (ArgumentException ex)
            {
                view.OnSubmitError(ex.Message);
            }
            catch (Exception ex)
            {
                view.OnSubmitError(ex.Message);

            }
        }


        public void GetItemsBySecond(string lastActivityId)
        {
            view.GetItemsComplete(itemService.GetLastActiviteVoteItemms(lastActivityId));
        }

        public void GetItemsBySecond(string lastActivityId, string keyword)
        {
            view.GetItemsComplete(itemService.GetLastActiviteItemms(lastActivityId, keyword));
        }

        public void GetProjects(string keyword)
        {
            view.GetProjectsComplete(projectService.FuzzySearch(keyword));
        }
    }
}
